package com.tianxiabuyi;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 异常过滤器
 * yanghx
 */
public class TxbyFilter implements Filter{

    private String errorPage;
    @Override
    public void destroy() {


    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        HttpServletRequest request = (HttpServletRequest) req;
        try {
            chain.doFilter(req, res);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("exception", e);//存储业务异常信息类
            request.setAttribute("reqpath", request.getRequestURI());//存储业务异常信息类
            request.getRequestDispatcher(errorPage).forward(request, response);//跳转到信息提示页面！！
            return;
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        //读取错误信息提示页面路径
        errorPage = config.getInitParameter("errorPage");
        if(null==errorPage || "".equals(errorPage)){
            System.out.println("没有配置错误信息跳转页面");
        }
    }

}
